<template>
  <div class="home flex justify-between" :class="[talkwebShow ? ' homeShow' : 'homeHide']">
    <zb-back @back="talkwebShowFun" :backshow="talkwebShow">
      <el-button class="wap-hide" @click="talkLinkFun({type:0})" link type="primary"><img src="@/assets/images/clear.png" class="clear-icon" alt=""></el-button>
    </zb-back>
    <div v-if="!talkwebShow" class="open-btn flex items-center cursor-pointer" :class="{show: !talkwebShow}" @click="openFun">
      <zb-icon-open :show="talkwebShow" />
      <span class="t">历史对话</span>
    </div>

    <div class="home-le bg-white" :class="{show: !talkwebShow}">
      <div class="home-le-wap">
        <div class="title">
          <span>AI智能对话</span>
          <zb-icon-open @click="openFun" :show="talkwebShow" />
        </div>
        <div class="btn" @click="talkLinkFun({type:1})"><icon name="local-icon-new" class="icon" :size="21" />新建对话</div>
      </div>
      <div class="history-title text-tx-secondary" :class="{on: !toggleHistory}" @click="toggleHistoryFun">
        历史对话
        <icon name="el-icon-ArrowDown" :size="16" />
      </div>
      
      <div class="scrollbar-box" :class="{on: toggleHistory}">
        <el-scrollbar height="100%">
          <dl class="list">
            <template v-if="isMobile">
              <zb-del-item v-for="(item, index) in talkList" :key="item.id">
                <template #content>
                  <div class="icon" :style="{background: colors[index].bg, color: colors[index].text}">
                    <span class="t">{{item.questions? item.questions.substr(0,1) : ''}}</span>
                    <img v-if="index === 0" src="@/assets/images/ai-kf.png" class="breathing wap" alt="">
                  </div>
                  <div class="div flex-1">
                    <div class="text-line1 name">{{item.questions}}</div>
                    <div class="intro text-tx-secondary text-line1">{{item.intro}}</div>
                  </div>
                  <div class="join-talk" @click.stop="talkLinkFun({type:2, e:null, id: item.id})"></div>
                </template>
                <template #actions>
                  <el-button link style="color:#fff" @click.stop="delTalk(item.id)">删除</el-button>
                </template>
              </zb-del-item>
            </template>
            <dd v-else class="swipe-container" :class="[curId === item.id ? 'on' : '']" v-for="(item, index) in talkList" :key="index"
              @click="talkLinkFun({type:2, e:$event, id: item.id})">
              <div class="icon" :style="{background: colors[index].bg, color: colors[index].text}">
                <span class="t">{{item.questions? item.questions.substr(0,1) : ''}}</span>
                <img v-if="index === 0" src="@/assets/images/ai-kf.png" class="breathing wap" alt="">
              </div>
              <div class="div flex-1">
                <div class="text-line1 name">{{item.questions}}</div>
                <div class="intro text-tx-secondary text-line1">{{item.intro}}</div>
              </div>
              <div class="more" data-type="1"><i></i></div>
              <div v-if="showDel && curId === item.id" class="el-popper el-popover">
                <div class="btn-list">
                  <div class="btns" @click="delTalk(item.id)">删除此对话</div>
                </div>
                <span class="el-popper__arrow" style=""></span>
              </div>
            </dd>
          </dl>
        </el-scrollbar>
        
      </div>
    </div>
    <div class="home-ri bg-white flex-1" :class="{show: talkwebShow}">
      <div class="mx-auto relative home-ri-content max-w">
        <p></p>
        <p></p>
        <!-- <zb-markdown :content="a" /> -->
        
        <!-- 对话内容 -->
        <zb-ai-talk1 ref="aiTalkRef" :id="curId" @sendFirst="getLists">
          <template #top>
            <div class="ai-kf-box">
              <h2 class="h2 mb-1"><img src="@/assets/images/ai-kf.png" class="breathing" alt=""><span
                  class="name">{{webTitle}}</span></h2>
              <div class="intro text-tx-regular"><b class="wap">{{webTitle}}</b>{{getWebsite.subtitle}}
              </div>
            </div>
          </template>
          
        </zb-ai-talk1>
      </div>
    </div>
  </div>
  <zb-ai-suport />
</template>
<script lang="ts" setup>
  import { topicListApi, delTopicApi, categoryApi } from '@/api/app';
  import { computed, ref } from 'vue';
  import { colors, isMobile, confirm } from '@/utils/util.ts'
  import { useAppStore } from '@/stores/app'
  const useStore = useAppStore();
  const getWebsite = computed(()=>{
    return useStore?.getWebsite
  });
  const webTitle = ref('');
  const category = () => {
    categoryApi({}).then((res:any)=>{
      console.log('res---',res)
      webTitle.value = res.Greetings
    })
  }
  category()
  // 历史对话收起
  const toggleHistory = ref(true);
  const toggleHistoryFun = () => {
    toggleHistory.value = !toggleHistory.value
  }
  
  const curId = ref();
  const showDel = ref(false)
  const talkList = ref();
  // 获取列表 -- add - 添加逻辑， del -- 类删除逻辑
  const getLists = async (id : number = 0, type:string='add') => {
    console.log('fasdfasas', id)
    topicListApi().then(res => {
      talkList.value = res;
      if(type == 'del'){
        curId.value = talkList.value.length > 0 ? talkList.value[0].id : 0;
      }else if(type == 'add'){
        curId.value = id;
      }
    })
  }
  getLists(0)
  const openFun = () => {
    talkwebShow.value = !talkwebShow.value;
  }
  // 删除对话
  const delTalk = async (id : number) => {
    console.log('-------------', id)
    await confirm('确认删除该对话吗？')
    await delTopicApi({ id: id });
    if ( isMobile ){
      aiTalkRef.value?.clearFun();
    }
    showDel.value = false;
    getLists(0, 'del');
  }
  const aiTalkRef = ref(); // 对话元素
  // type-   0-清除 1-创建 2-查看历史对话 
  const talkLinkFun = (data : any) => {
    console.log('111------------------', data)
    if (data.type == 0) {
      aiTalkRef.value?.clearFun();
    } else if (data.type === 1) {
      curId.value = '';
      talkwebShow.value = !talkwebShow.value;
      aiTalkRef.value?.clearFun();
    } else if (data.type === 2) {
      curId.value = data.id;
      aiTalkRef.value?.clearFun();
      aiTalkRef.value?.getTalkCon(data.id)
      if (data.e && data.e.target && data.e.target.dataset.type == 1) { // 弹出删除对话框
        showDel.value = true;
      } else {
        talkwebShow.value = true;
        showDel.value = false;
      }
    }
  }
  // 手机端返回
  const talkwebShow = ref(false); // ref(window.innerWidth > 769 ? true : false); // 展现聊天界面
  // 聊天界面的显示与隐藏
  const talkwebShowFun = () => {
    talkwebShow.value = !talkwebShow.value;
  }
</script>
<style lang="scss" scoped>
  $hvH: calc(var(--vh, 1vh) * 100);
  .btn-list {
    .btn {cursor: pointer;}
  }
  .home {height: $hvH; overflow: hidden; width: 100%;}
  .el-popover {left: 100%;position: absolute;top: 50px;margin-left: -50px;}

  .el-popper__arrow {
    position: absolute;bottom: 100%; left: 20px;transform: translateY(50%);
    &::before {background-color: #fff;}
  }
  $lepad: 10px;
  .open-btn {
    position: absolute;
    left: 100px;
    transition: all ease .3s;
    transform: translateX(400px);
    top: 10px;

    &.show {
      transform: translateX(0);
    }

    .t {
      margin-left: 6px;
    }
  }

  .home-le {
    width: 344px;
    border-right: 1px solid #ededed;
    padding: 0 20px;
    transition: all ease .3s;
    .history-title {
      margin-bottom: 10px;
      height: 60px;
      display: flex;
      align-items: center;
      justify-content: space-between; cursor: pointer;
      :deep(.el-icon){
        transition: all ease .3s;
      }
      &.on{
        :deep(.el-icon){
          transform: rotate(180deg);
        }
      }
    }
    .scrollbar-box{
      height: 0; transition: all ease 0.3s;
      &.on{height: calc(100% - 210px);}
      margin-left: -10px;
    }
    &.show {
      transform: translateX(-400px);
      width: 0px;
    }

    .title {
      display: flex;
      align-items: center;
      height: 66px;
      justify-content: space-between;
      font-size: 18px;
      margin-bottom: 10px;
      .icon {
        width: 24px; color: var(--el-color-primary);
      }
    }

    .btn {
      display: flex;
      align-items: center;
      cursor: pointer;
      padding: 0 10px;
      height: 40px;
      border-radius: 8px;
      background-color: var(--el-color-primary-light-9);
      border: 1px solid var(--el-color-primary-light-7);
      margin-bottom: 10px;
      color: var(--el-color-primary);

      .icon {
        margin-right: 10px;
      }
    }

    .list {
      .swipe-container,:deep(.swipe-container) {
        height: 60px; display: flex; align-items: center; cursor: pointer; margin-bottom: 4px;  color: #000; transition: all ease .3s; position: relative; padding-right: 50px; border-radius: 8px; padding: 0 $lepad;
        .icon {
          width: 40px;height: 40px;border-radius: 8px;background-color: #FFF4E5;font-size: 18px;font-weight: bold;display: flex;align-items: center;justify-content: center;margin-right: 8px;color: #FF9002;transition: all ease .3s;
        }
        .icon2 {display: none}
        .intro {display: none;}
        .el-icons {opacity: 0;visibility: hidden;}
        .more {
          position: absolute;
          right: 0;
          top: 0;
          display: none;
          align-items: center;
          justify-content: center;
          width: 50px;
          height: 100%;

          &::after,
          &::before,
          i {
            display: block;
            width: 4px;
            height: 4px;
            border-radius: 50%;
            background-color: #C4C4C4;
            content: '';
            margin: 0 2px;
          }
        }

      }

    }
  }

  .home-ri {
    display: flex;
    align-items: center;
    justify-content: center;
  }

  .ai-kf-box {
    margin-bottom: 50px;

    .h2 {
      font-size: 25px;
      color: #000;
      display: flex;
      align-items: center;
      justify-content: center;
      margin-bottom: 7px;
    }

    .breathing {
      width: 50px;
      margin-right: 10px;
    }
  }

  :deep(.home-ri-top) {
    background-color: transparent !important;
    justify-content: center;
  }

  @media only screen and (min-width: 770px) {
    .list {
      .swipe-container,:deep(.swipe-container) {
        
        &.on,&:hover {
          background-color: #F5F5F5 !important;
          .icon {
            color: var(--el-color-primary) !important;
            background-color: #fff !important;
          }
          .more {
            display: flex;
          }
        }
      }
    }
  }

  // 手机端样式
  $topH: calc($hvH - 60px);
  @media only screen and (max-width: 769px) {
    .ai-kf-box {
      .name { display: none; }
      .wap { display: inline; }
      .h2 {background-color: var(--el-color-primary-light-9); margin: 0 auto 0.4rem; border-radius: 0.56rem;height: 1.8rem; width: 1.8rem;}
      .breathing { width: 1.4rem; margin: 0; }
      .intro { 
        color: #000; font-size: 0.32rem; border-radius: 0.24rem; padding: 0.2rem 0.3rem; background-color: #F7F7F7; 
        .wap{display: block;}
        
      }
      
    }
    .home-ri-content { height: 100%;}
    .open-btn { display: none; }
    .home-le {
      width: 100% !important;
      padding: 0;
      border-radius: 0px;
      display: none;
      position: fixed;
      z-index: var(--zindex1);
      left: 0;
      top: 0;
      height: calc(100% - env(safe-area-inset-bottom) - 1.4rem);
      .history-title{display: none !important;}
      .scrollbar-box{
        margin-top: 60px; margin-left: 0;
        height: calc(100% - 60px) !important;
      }
      .home-le-wap {
        height: 60px;display: flex;align-items: center;padding: 0 var(--wap-pad);font-size: 0.26rem;position: fixed;width: 100%;left: 0;z-index: var(--zindex2);background: #fff;
        .name {text-align: center;display: block;font-size: 0.32rem;}
        .web {display: none;}
        .icon {margin-right: 0.15rem;}
        .btn {padding: 0; font-weight: bold;}
      }
      .btn {margin-bottom: 0;color: #000;background-color: transparent;border: none;}
      .title {display: none;}
      &.show {transform: translateX(0);display: block;}
      .list {
        .title {display: none;}
        .swipe-container,:deep(.swipe-container) {
          height: 1.52rem; padding-right: 0; border-radius: 0px;padding: var(--wap-pad);
          .join-talk{position: absolute; left: 0; top: 0; z-index: 980; width: 100%; height:100%}
          .content{display: flex; align-items: center;}
          .name {font-weight: bold;font-size: 0.34rem;margin-bottom: 0.06rem;}
          &:nth-child(1) {
            background-color: var(--el-color-primary-light-9);
            .icon {
              background-color: var(--el-color-primary-light-8); 
              .t {display: none;}
            }
          }
          .intro {font-size: 0.26rem;display: -webkit-box; height: 0.3rem;}
          .icon {
            width: 1.12rem;min-width: 1.12rem; height: 1.12rem;border-radius: 0.36rem;font-size: 0.49rem;text-transform: uppercase;
            .breathing {width: 0.9rem;}
          }
        }
      }
    }

    .open-left {
      display: block;
    }

    .home-ri {
      padding: 0;
      border-radius: 0px;
      display: none;

      &.show {
        display: flex;
      }
    }

    :deep(.talk-content.on) {
      .el-scrollbar__wrap {
        padding: var(--wap-pad);
      }
    }

    .home {
      position: fixed;
      left: 0;
      top: 0;
      height: auto;
      bottom: var(--bottomH);
      width: 100%;
      z-index: calc(var(--zindex) + 1);
      
      .wap-creat{
        display: flex; align-items: center;
        .name{margin-left: 0.1rem; font-size: 0.32rem;}
        :deep(svg){width: 0.32rem !important; height:0.32rem !important;}
      }
      .wap-tit{font-size: 0.34rem; text-align: center; padding-right: 0.6rem; line-height: 1;}
      &.homeShow {
        bottom: var(--bottomIpx);
        top: 60px;
        .wap-show{display: none;}
      }
      &.homeHide{
        .wap-hide{display: none;}
      }

      :deep(.bottom-box) {
        bottom: var(--bottomIpx);
        box-shadow: none;
      }
    }
  }
</style>